package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl2 implements StudentService {

    @Override
    public PageInfo findPage(int pageNum, int pageSize) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            //在查询所有之前，要设置分页信息
            PageHelper.startPage(pageNum,pageSize);
            //分页查询结果集
            List<Student> students = proxyStudentMapper.findAll();

            MybatisUtil.commit(sqlSession);

            return new PageInfo(students);
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public List<Student> findAll() {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            List<Student> students = proxyStudentMapper.findAll();

            MybatisUtil.commit(sqlSession);

            return students;
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public Student findById(String id) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            Student student = proxyStudentMapper.findById(id);

            MybatisUtil.commit(sqlSession);

            return student;
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public void save(Student student) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            proxyStudentMapper.save(student);

            MybatisUtil.commit(sqlSession);

        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void update(Student student) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            proxyStudentMapper.update(student);

            MybatisUtil.commit(sqlSession);

        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.openSession();
            StudentMapper proxyStudentMapper = sqlSession.getMapper(StudentMapper.class);

            proxyStudentMapper.delete(id);

            MybatisUtil.commit(sqlSession);

        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback(sqlSession);
        }finally {
            MybatisUtil.close(sqlSession);
        }
    }
}
